<?php
 include("db.php");
 $db=new Db(); 

function row($table,$selectfield="*",$where="",$limit="",$order="")
{
	$db=new Db();
	$sql="SELECT ".$selectfield." FROM ".$table;
	if($where!="")
	{
		$wherefield=" where ".$where;
		$sql.=$wherefield;
	}
	if($order!="")
	{
		$sql.=" order by ".$order;
	}
	if($limit!="")
	{
		$sql.=" limit ".$limit;
	}
	//echo $sql;
	$result=$db->fetchRow($sql);
	return $result;
}   
/*
  author libin
  $array传入的含有用户名、密码的数组,$array[0]为用户名,$array[1]为密码,$table用户表名,	$url为网页跳转的路径
*/		

function login($array,$table,$url)
{
	$db=new Db(); 
	$users=array_keys($array);
	$user=$db->fetchRow("SELECT ".$users[1]." FROM ".$table." where '$users[0]'='$array[v_username]'");
	if($user[v_password]==$array[v_password])
	{   
		echo "<script>alert('登录成功！');</script>";
		echo "<script>window.location='$url'</script>";
	}
	else
	{
	echo "<script>alert('用户名或密码不正确');</script>";
	echo '<script>history.back()</script>';
	}
}
function dou($No,$id)
{
	$sql="UPDATE vip_info SET v_dou=v_dou+$No WHERE v_id=$id";
	//echo $sql;
	$db=new Db(); 
	$db->query($sql);
}
		
function insert($array,$table,$url="")
{
	$db=new Db(); 
	foreach ($array as $fieldName => $fieldValue) 
	{		
		$fieldNameList[] = $fieldName;
		$fieldValueList[] = '\'' . $fieldValue . '\'';
	}
	$fieldName = implode(',', $fieldNameList);
	$fieldValue = implode(',', $fieldValueList);
	$sql = 'INSERT INTO ' . $table . '('
				. $fieldName . ') VALUES (' . $fieldValue . ')';
	//echo $sql;
	$db->query($sql);
	if($url!="")
	{
		echo '<meta http-equiv="refresh" content="0;URL='.$url.'" />';
	}
}

function update($array,$where,$table,$url="")
{
	//print_r($array);
	$db=new Db(); 
	$whereForUpdate = ' WHERE ' . $where;
	$fieldNameValueList=array();
	foreach ($array as $fieldName => $fieldValue) {
		
			$fieldNameValueList[] = $fieldName . '=\'' . $fieldValue . '\'';
			
	}
	
	$fieldNameValue = implode(',', $fieldNameValueList);
	$sql = 'UPDATE ' . $table . ' SET ' . $fieldNameValue . $whereForUpdate;
	//echo $sql;
	$db->query($sql);
	if($url!="")
	{
		echo '<meta http-equiv="refresh" content="0;URL='.$url.'" />';
	}
}

function select($table,$selectfield="*",$where="",$limit="",$order="")
{
	$db=new Db();
	$sql="SELECT ".$selectfield." FROM ".$table;
	if($where!="")
	{
		$wherefield=" where ".$where;
		$sql.=$wherefield;
	}
	if($order!="")
	{
		$sql.=" order by ".$order;
	}
	if($limit!="")
	{
		$sql.=" limit ".$limit;
	}
	//echo $sql;
	$result=$db->fetchRows($sql);
	return $result;
}

function rowNum($table,$selectfield="*",$where="",$limit="",$order="")
{
	$db=new Db();
	$sql="SELECT ".$selectfield." FROM ".$table;
	if($where!="")
	{
		$wherefield=" where ".$where;
		$sql.=$wherefield;
	}
	if($order!="")
	{
		$sql.=" order by ".$order;
	}
	if($limit!="")
	{
		$sql.=" limit ".$limit;
	}
	//echo $sql;
	$result=$db->rowNum($sql);
	return $result;
}

function del($table,$value,$field,$url="")
{
	$db=new Db();
	$sql="delete from ".$table." where ".$field."=".$value;
	$db->query($sql);
	//echo $sql;
	if($url!="")
	{
		echo '<meta http-equiv="refresh" content="0;URL='.$url.'" />';
	}
}
function sendletter($array,$sendid="2")
{
	$array1=array("l_sendid"=>$sendid,"l_receviceid"=>$array[receviceid],"l_content"=>$array[content],"l_title"=>$array[title],"l_date"=>$array[dates]);
	insert($array1,"vip_letter");
}

function show_file(){
	$folder_name = "../db/backup";
	$d_open = opendir($folder_name);
	$num = 0;
	while($file = readdir($d_open)){
		$filename[$num] = $file;
		$num++; 
	}
	closedir($d_open);
	return $filename;
}
?>